package game.engine;
import graphics.objects.GCircle;
import graphics.objects.GPolygon;
import graphics.objects.GRect;
import graphics.objects.GraphicsObject;
import graphics.system.Color;
import physics.Body;
import physics.shapes.Circle;
import physics.shapes.Polygon;
import physics.shapes.Shape;
import tools.Point;
public class BodyToGraphics {
	public static GraphicsObject get(Body b, Color c) {
		Shape p = b.shape;
		if (p instanceof Polygon) {
			Polygon poly = (Polygon) p;
			if (checkRect(poly)) {
				return getRect(c, poly);
			}
			Point[] points = new Point[poly.numPoints];
			for (int i = 0; i < poly.numPoints; i++) {
				points[i] = poly.points[i].sub(poly.pos);
			}
			GPolygon gpoly = new GPolygon(b.pos, poly.pointsOffset, -b.apos, c);
			gpoly.glowColor = new Color(0, 0, 0, 1);
			return gpoly;
		}
		if (p instanceof Circle) {
			Circle circ = (Circle) p;
			return new GCircle(circ.pos, circ.radius, 0.0f, c);
		}
		assert (false);
		return null;
	}
	private static GraphicsObject getRect(Color c, Polygon poly) {
		assert (checkRect(poly)); // This has to be true.
		float width = poly.points[0].sub(poly.points[1]).length();
		float height = poly.points[1].sub(poly.points[2]).length();
		float alpha = poly.lines[0].angle();
		GRect rect = new GRect(poly.pos, width, height, alpha, c);
		rect.glowColor = new Color(0, 0, 0, 1);
		return rect;
	}
	private static boolean checkRect(Polygon poly) {
		if (poly.numPoints != 4)
			return false;
		float THRESHOLD = 0.0001f;
		for (int i = 0; i < poly.numPoints - 1; i++) {
			// Jede Linie senkrecht zu der naechsten? => Senkrecht
			if (Math.abs(poly.lines[i].mul(poly.lines[i + 1])) >= THRESHOLD) {
				return false;
			}
		}
		return true;
	}
}
